package org.jflame.auth.authc;

import org.jflame.auth.LoginUser;
import org.jflame.auth.authz.Permit;
import org.jflame.auth.authz.SimpleRole;
import org.jflame.commons.util.CollectionHelper;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class SimpleLoginUser extends LoginUser {

    private static final long serialVersionUID = 1L;

    private String id;
    private String userName;
    private Set<String> roles = new HashSet<>();
    private Set<Permit> permits = new HashSet<>();
    private Serializable userDetail;

    public SimpleLoginUser() {
    }

    public SimpleLoginUser(String userName) {
        this.userName = userName;
    }

    public SimpleLoginUser(String id, String userName) {
        this.id = id;
        this.userName = userName;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getUserName() {
        return userName;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Set<String> getRoles() {
        return roles;
    }

    public void setRoles(final Set<String> roles) {
        this.roles = roles;
    }

    public void addRoles(final String... _roles) {
        Collections.addAll(this.roles, _roles);
    }

    public void addRoles(final Set<? extends SimpleRole> _roles) {
        if (CollectionHelper.isNotEmpty(_roles)) {
            for (SimpleRole r : _roles) {
                roles.add(r.getRoleCode());
                permits.addAll(r.getPermissions());
            }
        }
    }

    public void setPermits(final Set<Permit> permitSet) {
        this.permits = permitSet;
    }

    @Override
    public Set<Permit> getPermits() {
        return this.permits;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends Serializable> T getUserDetail() {
        return (T) userDetail;
    }

    public void setUserDetail(Serializable userDetail) {
        this.userDetail = userDetail;
    }

}
